<!-- Copyright (c) 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->

<polymer-element name="cr-title-handler" attributes="titlePrefix titleSuffix">
    <template>
        <style>
            :host { display: none; }
        </style>
    </template>
    <script>
        Polymer({
            created: function() {
                this.titlePrefix = "";
                this.titleSuffix = "";
                this.handleTitleChange = this.handleTitleChange.bind(this);
            },
            attached: function() {
                document.addEventListener("title-change", this.handleTitleChange);
            },
            detached: function() {
                document.removeEventListener("title-change", this.handleTitleChange);
            },
            handleTitleChange: function(event) {
                var value = event.detail.value || "";
                var title = this.titlePrefix;
                if (title.length && value.length)
                    title += ": ";
                if (value.length)
                    title += value;
                if (this.titleSuffix)
                    title += " - " + this.titleSuffix;
                document.title = title;
            },
        });
    </script>
</polymer-element>
